<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>6- 光照材质</title>
</head>

<body>

</body>
<script type="importmap">
    {
        "imports": {
            "three": "./build/three.module.js",
            "three/addons/": "./examples/jsm/"
        }
    }
</script>
<script type="module">
    import * as THREE from 'three';
    import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
    import Stats from 'three/addons/libs/stats.module.js';

    const width = window.innerWidth;
    const height = window.innerHeight;

    // 创建场景 相机 渲染器 
    const scene = new THREE.Scene()
    const renderer = new THREE.WebGLRenderer()
    renderer.setSize(width, height)
    const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
    camera.position.set(200, 100, 800);
    camera.lookAt(0, 0, 0);


    // 添加辅助坐标轴
    const axesHelper = new THREE.AxesHelper(150);
    scene.add(axesHelper)


    // 创建几何体
    const geometry = new THREE.BoxGeometry(100, 100, 100);
    const material = new THREE.MeshPhongMaterial({
        color: 0xff0000,
        shininess: 20, //高光部分的亮度，默认30
        specular: "blue", //高光部分的颜色
    })
    const mesh = new THREE.Mesh(geometry, material)
    mesh.position.set(0, 0, 0);
    scene.add(mesh)


    // 平行光
    const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
    directionalLight.position.set(100, 100, 70);
    // directionalLight.target = mesh;
    directionalLight.target.position.set(0, 20, 0);
    scene.add(directionalLight);
    // 辅助光源
    const dirLightHelper = new THREE.DirectionalLightHelper(directionalLight, 10);
    scene.add(dirLightHelper);


    // 轨道控制器
    const controls = new OrbitControls(camera, renderer.domElement);
    controls.addEventListener('change', function () {
        renderer.render(scene, camera); //执行渲染操作
    });//监听鼠标、键盘事件

    // 添加
    scene.add(mesh)
    document.body.appendChild(renderer.domElement);
    renderer.render(scene, camera)

    // 渲染函数
    function render() {

        renderer.render(scene, camera); //执行渲染操作

        mesh.rotateY(0.01);//每次绕y轴旋转0.01弧度

        requestAnimationFrame(render);//请求再次执行渲染函数render，渲染下一帧

    }

    render();

</script>

</html>